﻿using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Web.Script.Serialization;

namespace NxStoreManage
{
    public class ServerFileUpDownload
    {
        public static UpFileResult Up(List<string> filePath, string postUrl, string serverDir)
        {
            using (HttpClient client = new HttpClient())
            {
                var content = new MultipartFormDataContent
                {
                    //添加字符串参数，参数名为qq
                    { new StringContent(serverDir), "saveDir" }
                };
                foreach (var item in filePath)
                {
                    int start = item.LastIndexOf('\\');
                    string name = item.Substring(start + 1);
                    //添加文件参数，参数名为files，文件名为name
                    content.Add(new ByteArrayContent(File.ReadAllBytes(item)), "files", name);
                }
                var json = new JavaScriptSerializer();
                return json.Deserialize<UpFileResult>(client.PostAsync(postUrl, content).Result.Content.ReadAsStringAsync().Result);
            }
        }

        public static UpFileResult Up(string filePath, string postUrl, string serverDir, string newFileName = null)
        {
            using (HttpClient client = new HttpClient())
            {
                var content = new MultipartFormDataContent
                {
                    //添加字符串参数，参数名为qq
                    { new StringContent(serverDir), "saveDir" }
                };
                string name;
                if (newFileName == null)
                {
                    int start = filePath.LastIndexOf('\\');
                    name = filePath.Substring(start + 1);
                }
                else
                {
                    name = newFileName;
                }
                //添加文件参数，参数名为files，文件名为name
                content.Add(new ByteArrayContent(File.ReadAllBytes(filePath)), "files", name);
                var json = new JavaScriptSerializer();
                return json.Deserialize<UpFileResult>(client.PostAsync(postUrl, content).Result.Content.ReadAsStringAsync().Result);
            }
        }

        /// <summary>
        /// 下载文件(显示进度)
        /// </summary>
        /// <param name="URL">     </param>
        /// <param name="filename"></param>
        /// <param name="prog">    </param>
        public static bool DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog)
        {
            try
            {
                System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
                System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                long totalBytes = myrp.ContentLength;

                if (prog != null)
                {
                    prog.Maximum = (int)totalBytes;
                }

                System.IO.Stream st = myrp.GetResponseStream();
                System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                long totalDownloadedByte = 0;
                byte[] by = new byte[2048];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    System.Windows.Forms.Application.DoEvents();
                    so.Write(by, 0, osize);

                    if (prog != null)
                    {
                        prog.Value = (int)totalDownloadedByte;
                    }
                    osize = st.Read(by, 0, (int)by.Length);
                }
                so.Close();
                st.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

    public class UpFileResult
    {
        public long Count { get; set; }
        public int Size { get; set; }
        public int SucessCount { get; set; }
        public int ErrCount { get; set; }
        public int Code { get; set; }
        public string Msg { get; set; }
    }
}